<?php

namespace app\store\service\statistics\data;

use app\common\library\helper;
use app\common\service\Basics as BasicsService;
use app\store\model\User as UserModel;
use app\store\model\Order as OrderModel;
use app\store\model\Goods as GoodsModel;
use app\store\model\recharge\Order as RechargeOrderModel;
use app\common\enum\order\Status as OrderStatusEnum;
use app\common\enum\order\PayStatus as PayStatusEnum;
use app\common\enum\recharge\order\PayStatus as RechargePayStatusEnum;

/**
 * 数据概况
 * Class Survey
 * @package app\store\service\statistics\data
 */
class Survey extends BasicsService
{
    /**
     * 获取数据概况
     * @param $startDate
     * @param $endDate
     * @return array
     * @throws \think\Exception
     */
    public function getSurveyData($startDate = null, $endDate = null)
    {
        return [
            // 用户数量
            'user_total' => $this->getUserTotal($startDate, $endDate),
            // 消费人数
            'consume_users' => $this->getConsumeUsers($startDate, $endDate),
            // 付款订单数
            'order_total' => $this->getOrderTotal($startDate, $endDate),
            // 付款订单总额
            'order_total_money' => $this->getOrderTotalMoney($startDate, $endDate),
            // 商品总量
            'goods_total' => $this->getGoodsTotal($startDate, $endDate),
            // 用户充值总额
            'recharge_total' => $this->getRechargeTotal($startDate, $endDate),
            // 门店付款
            'shop_total' => $this->getShopTotal($startDate, $endDate),
            // 微信支付进货款
            'supplier_total' => $this->getSupplierTotal($startDate, $endDate),
            // 线下支付进货款
            'supplierXia_total' => $this->getXianXiaSupplierTotal($startDate, $endDate),
            // 赠送用户积分
            'userPoints_total' => $this->getUserPointsTotal($startDate, $endDate),
            // 库存商品
            'stockNum_total' => $this->getStockNumTotal($startDate, $endDate),
            // 库存总额
            'outPrice_total' => $this->getOutPriceTotal($startDate, $endDate),
        ];
    }

    /**
     * 获取用户总量
     * @param null $startDate
     * @param null $endDate
     * @return string
     * @throws \think\Exception
     */
    private function getUserTotal($startDate = null, $endDate = null)
    {
        $model = new UserModel;
        if (!is_null($startDate) && !is_null($endDate)) {
            $model->where('create_time', '>=', strtotime($startDate))
                ->where('create_time', '<', strtotime($endDate) + 86400);
        }
        $value = $model->where('is_delete', '=', '0')->count();
        return number_format($value);
    }

    /**
     * 消费人数
     * @param null $startDate
     * @param null $endDate
     * @return string
     * @throws \think\Exception
     */
    public function getConsumeUsers($startDate = null, $endDate = null)
    {
        $model = new OrderModel;
        if (!is_null($startDate) && !is_null($endDate)) {
            $model->where('pay_time', '>=', strtotime($startDate))
                ->where('pay_time', '<', strtotime($endDate) + 86400);
        }
        $value = $model->field('user_id')
            ->where('pay_status', '=', PayStatusEnum::SUCCESS)
            ->where('order_status', '<>', OrderStatusEnum::CANCELLED)
            ->where('is_delete', '=', '0')
            ->group('user_id')
            ->count();
        return number_format($value);
    }

    /**
     * 获取订单总量
     * @param null $startDate
     * @param null $endDate
     * @return string
     * @throws \think\Exception
     */
    private function getOrderTotal($startDate = null, $endDate = null)
    {
        return number_format((new OrderModel)->getPayOrderTotal($startDate, $endDate));
    }

    /**
     * 付款订单总额
     * @param null $startDate
     * @param null $endDate
     * @return string
     */
    private function getOrderTotalMoney($startDate = null, $endDate = null)
    {
        return helper::number2((new OrderModel)->getOrderTotalPrice($startDate, $endDate));
    }

    /**
     * 获取商品总量
     * @param null $startDate
     * @param null $endDate
     * @return int|string
     * @throws \think\Exception
     */
    private function getGoodsTotal($startDate = null, $endDate = null)
    {
        $model = new GoodsModel;
        if (!is_null($startDate) && !is_null($endDate)) {
            $model->where('create_time', '>=', strtotime($startDate))
                ->where('create_time', '<', strtotime($endDate) + 86400);
        }
        $value = $model->where('is_delete', '=', 0)->count();
        return number_format($value);
    }

    /**
     * 用户充值总额
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    private function getRechargeTotal($startDate = null, $endDate = null)
    {
        $model = new RechargeOrderModel;
        if (!is_null($startDate) && !is_null($endDate)) {
            $model->where('pay_time', '>=', strtotime($startDate))
                ->where('pay_time', '<', strtotime($endDate) + 86400);
        }
        $value = $model->where('pay_status', '=', RechargePayStatusEnum::SUCCESS)
            ->sum('actual_money');
        return helper::number2($value);
    }
    /**
     * 门店付款总金额
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    private function getShopTotal($startDate = null, $endDate = null)
    {
        $where = 'pay_status = 20';
        if (!is_null($startDate) && !is_null($endDate)) {
            $endDate = strtotime($endDate) + 86400;
            $where .= ' and create_time >= '.strtotime($startDate).' and create_time < '.$endDate;
        }
//        echo $where;
        $value = db('pay_order')
            ->where($where)
            ->sum('pay_price');
        return helper::number2($value);
    }
    /**
     * 供应商微信支付货款总金额
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    private function getSupplierTotal($startDate = null, $endDate = null)
    {
        $where = 'status = 1 and pay_type = "微信付款"';
        if (!is_null($startDate) && !is_null($endDate)) {
            $endDate = strtotime($endDate) + 86400;
            $where .= ' and add_time >= '.strtotime($startDate).' and add_time < '.$endDate;
        }
        $value = db('supplier_huokuan')
            ->where($where)
            ->sum('money');
        return helper::number2($value);
    }
    /**
     * 供应商线下付款货款总金额
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    private function getXianXiaSupplierTotal($startDate = null, $endDate = null)
    {
        $where = 'status = 1 and pay_type = "线下付款"';
        if (!is_null($startDate) && !is_null($endDate)) {
            $endDate = strtotime($endDate) + 86400;
            $where .= ' and add_time >= '.strtotime($startDate).' and add_time < '.$endDate;
        }
        $value = db('supplier_huokuan')
            ->where($where)
            ->sum('money');
        return helper::number2($value);
    }
    /**
     * 用户赠送总积分
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    private function getUserPointsTotal($startDate = null, $endDate = null)
    {
        $where = '';
        if (!is_null($startDate) && !is_null($endDate)) {
            $endDate = strtotime($endDate) + 86400;
            $where .= ' and create_time >= '.strtotime($startDate).' and create_time < '.$endDate;
        }
        $value = db('user')
            ->where($where)
            ->sum('points');
        return helper::number2($value);
    }
    /**
     * 库存商品
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    private function getStockNumTotal($startDate = null, $endDate = null)
    {
        $where = '';
        if (!is_null($startDate) && !is_null($endDate)) {
            $endDate = strtotime($endDate) + 86400;
            $where .= ' and create_time >= '.strtotime($startDate).' and create_time < '.$endDate;
        }
        $value = db('goods_stock')
            ->where($where)
            ->sum('stock_num');
        return number_format($value);
    }
    /**
     * 库存商品
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    private function getOutPriceTotal($startDate = null, $endDate = null)
    {
        $where = '';
        if (!is_null($startDate) && !is_null($endDate)) {
            $endDate = strtotime($endDate) + 86400;
            $where .= ' and create_time >= '.strtotime($startDate).' and create_time < '.$endDate;
        }
        $sql = 'SELECT SUM(s) as OutPriceTotal FROM (SELECT *,(join_price*stock_num) AS s FROM yoshop_goods_stock) d';
        $value = db('goods_stock')->query($sql);
//            ->where($where)
//            ->sum('out_price * stock_num');
        return helper::number2($value[0]['OutPriceTotal']);
    }

}